package com.sskj.cndragon.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.design.widget.BottomSheetDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.bulong.rudeness.RudenessScreenHelper;
import com.sskj.cndragon.R;
import com.sskj.cndragon.bean.AssetListBean;
import com.sskj.cndragon.bean.RecordListBean;
import com.sskj.cndragon.bean.WithDrawBean;
import com.sskj.cndragon.view.ToolBarLayout;
import com.sskj.common.adapter.ItemClickSupport;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.NumberUtil;
import com.sskj.common.util.SPUtil;
import com.sskj.common.util.TimeFormatUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.SPConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.util.ArithUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @author Hey
 * Create at  2019/06/26
 */
public class WithDrawActivity extends BaseActivity<WithDrawPresenter> {

    private static SlimAdapter<WithDrawBean.StockBean> slimAdapter;
    @BindView(R.id.toolbar)
    ToolBarLayout toolbar;
    @BindView(R.id.tv_fhb_money)
    TextView tvFhbMoney;
    @BindView(R.id.tv_fhb_cny)
    TextView tvFhbCny;
    @BindView(R.id.view_choose_dragon)
    View viewChooseDragon;
    @BindView(R.id.tv_dragon_name)
    TextView tvDragonName;
    @BindView(R.id.tv_withdraw)
    TextView tvWithdraw;
    @BindView(R.id.et_transfer_account)
    EditText etTransferAccount;
    @BindView(R.id.et_pay_pwd)
    EditText etPayPwd;
    @BindView(R.id.btn_withdraw)
    Button btnWithdraw;

    private String rate;
    private String dragonID;
    private List<WithDrawBean.StockBean> dragonBeans = new ArrayList<>();

    @Override
    public WithDrawPresenter getPresenter() {
        return new WithDrawPresenter();
    }

    @Override
    public void initView() {
        toolbar.setTitle("提现");
        toolbar.setLeftButtonOnClickListener(v -> finish());
        setInputListener();
    }

    private void setInputListener() {
        etTransferAccount.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String input = getText(etTransferAccount);
                if (isEmpty(input)) {
                    input = "0";
                }
                if (isEmpty(rate)) {
                    return;
                }
                tvWithdraw.setText(String.valueOf(ArithUtil.txfloat(Double.valueOf(input), Double.valueOf(rate))));
            }
        });
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.app_activity_with_draw;
    }

    @Override
    public void initData() {
        mPresenter.getWithdrawPre(SPUtil.get(SPConfig.ID, ""));
        ClickUtil.click(300, btnWithdraw, () -> {
            if (isEmpty(etPayPwd)) {
                ToastUtil.showShort(getString(R.string.qingshuruzhifumima));
                return;
            }
            if (isEmpty(dragonID)) {
                ToastUtil.showShort("获取龙池信息失败");
                return;
            }
            if (isEmpty(etTransferAccount)) {
                ToastUtil.showShort("请输入提现数量");
                return;
            }
            if (isEmpty(etPayPwd)) {
                ToastUtil.showShort("请输入交易密码");
                return;
            }
            mPresenter.withDraw(SPUtil.get(SPConfig.ID, ""), dragonID, getText(etTransferAccount), getText(etPayPwd));
        });
        ClickUtil.click(300, viewChooseDragon, () -> {
            if (dragonBeans.size() == 0) {
                ToastUtil.showShort("获取龙池信息失败");
                return;
            }

            chooseDragons(this, dragonBeans, stockBean -> {
                tvDragonName.setText(stockBean.getName());
                dragonID = stockBean.getId();
                etTransferAccount.setHint(NumberUtil.keepNoZero(stockBean.getMinPrice()) + "-" + NumberUtil.keepNoZero(stockBean.getMaxPrice()));
            });
        });
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, WithDrawActivity.class);
        context.startActivity(intent);
    }

    public void getWithdrawPreOK(WithDrawBean data) {
        if (data == null) {
            return;
        }
        rate = data.getRate();
        tvFhbMoney.setText(data.getFund() + " FHB");
        tvFhbCny.setText("≈ ￥ " + ArithUtil.mul(Double.valueOf(data.getFund()), Double.valueOf(data.getRate())));
        if (data.getStockList() != null && data.getStockList().size() > 0) {
            dragonBeans = data.getStockList();
            dragonID = data.getStockList().get(0).getId();
            tvDragonName.setText(data.getStockList().get(0).getName());
            etTransferAccount.setHint(NumberUtil.keepNoZero(data.getStockList().get(0).getMinPrice()) + "-" + NumberUtil.keepNoZero(data.getStockList().get(0).getMaxPrice()));
        }
    }

    public static void chooseDragons(Activity activity, List<WithDrawBean.StockBean> datas, OnSure onSure) {
        BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(activity);
        View inflate = activity.getLayoutInflater().inflate(R.layout.dialog_choose_dragon, null, false);
        RecyclerView recyclerView = inflate.findViewById(com.sskj.lib.R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(activity));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(activity)
                .setPaintWidth((int) RudenessScreenHelper.pt2px(activity, 1))
                .setPaintColor(Color.TRANSPARENT)
                .setFirstDraw(false)
                .setLastDraw(false));
        slimAdapter = SlimAdapter.create().register(R.layout.item_dragon, new SlimInjector<WithDrawBean.StockBean>() {
            @Override
            public void onInject(WithDrawBean.StockBean data, IViewInjector injector, List payloads) {
                injector.text(R.id.textView, data.getName())
                        .clicked(R.id.textView, v -> {
                            onSure.onSure(data);
                            bottomSheetDialog.dismiss();
                        });
            }
        }).attachTo(recyclerView).updateData(new ArrayList());
        slimAdapter.updateData(datas);
//        ItemClickSupport.addTo(recyclerView).setOnItemClickListener((recyclerView1, position, v) -> {
//            onSure.onSure(datas.get(position));
//            bottomSheetDialog.dismiss();
//        });
        bottomSheetDialog.setContentView(inflate);
        try {
            // hack bg color of the BottomSheetDialog
            ViewGroup parent = (ViewGroup) inflate.getParent();
            parent.setBackgroundResource(android.R.color.transparent);
        } catch (Exception e) {
            e.printStackTrace();
        }
        bottomSheetDialog.setCancelable(true);
        bottomSheetDialog.setCanceledOnTouchOutside(true);
        bottomSheetDialog.show();
    }

    private interface OnSure {
        void onSure(WithDrawBean.StockBean stockBean);
    }
}
